#include "Tree.h"

Tree::Tree(Scene3D::Geometry *tree_model)
{
	this ->tree_model = tree_model;
	position = new Vec3((float)(rand()%100 - 50), 6.0f, (float)(rand()%100 - 50));

	glGenBuffersARB(2, vbo_id);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_id[0]);
	glBufferDataARB(
		GL_ARRAY_BUFFER_ARB,
		this ->tree_model ->meshes[0] ->verticles_count * sizeof(Vec3),
		this ->tree_model ->meshes[0] ->verticles,
		GL_STATIC_DRAW_ARB);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_id[1]);
	glBufferDataARB(
		GL_ARRAY_BUFFER_ARB,
		this ->tree_model ->meshes[0] ->verticles_count * sizeof(Vec3),
		this ->tree_model ->meshes[0] ->normals,
		GL_STATIC_DRAW_ARB);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
	glEnable(GL_SPECULAR);
}

Tree::~Tree(void)
{
}

bool Tree::IsVisible(Vec3 view_position, Vec3 view_targer)
{
	return true;
}

void Tree::Process(float ms)
{
}

void Tree::Render()
{
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
	glDisable(GL_TEXTURE_2D);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	//glEnableClientState( GL_TEXTURE_COORD_ARRAY );

	glPushMatrix();
	glTranslatef(position ->x, position ->y, position ->z);
	
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_id[1]);
	glNormalPointer(GL_FLOAT, 0, NULL);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_id[0]);
	glVertexPointer(3, GL_FLOAT, 0, NULL);
	//glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

	glDrawArrays(GL_TRIANGLES, 0, tree_model ->meshes[0] ->verticles_count);

	glPopMatrix();

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	//glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
